// 路由拦截器
import router from './router'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// 路由拦截器
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (window.axiosPromiseCancel) {
    // 页面跳转时，终止上一页面请求
    window.axiosPromiseCancel.forEach((e) => {
      e()
    })
    window.axiosPromiseCancel = []
  }
  // 登录判断
  if (to.path === '/login') {
    sessionStorage.removeItem('user')
  }
  const user = sessionStorage.getItem('user')
  if (!user && to.path !== '/login') {
    next({ path: '/login' })
  } else {
    next()
  }
})
router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
